<?php
namespace Admin_\Controller;
use Think\Controller;
class LoginController extends Controller {

    /**
     * 检查登录
     */
    public function check_login(){
        $uid = session('user_id');
        if($uid)
            return true;
        return false;
    }

    /**
     * 去登录
     */
    public function to_login(){
        if(IS_POST){
            //检查用户
            $name = I('post.name');
            $passwd = I('post.passwd');
            if($name && $passwd){
                $user_info = M('User')->where(array(
                    'name' => $name,
                    'status' => 1,
                ))->find();
                if($user_info && $this->check_sign($passwd,$user_info['passwd'])) {
                    session('user_id',$user_info['id']);
                    session('user_info',$user_info);
                    M('user')->where(array('id'=>$user_info['id']))->setField('last_time',time());
                    JSON(array('code'=>100,'message'=>'登录成功','data'=>array('go_url'=>U('Index/index'))));
                }
            }
            JSON(array('code'=>101,'message'=>'密码错误'));
        }
        else
        $this->display('Login/login');
    }
    public function out(){
        session(null);
        $this->to_login();
    }

    public function make_sign($passwd){
        return md5($passwd . C('PASSWD_SALT'));
    }
    public function check_sign($passwd,$sign){
        return $this->make_sign($passwd) == $sign;
    }
}
